//
// Created by layty on 2023/3/10.
//
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}

#include <memory>
#include <iostream>
// #include <string_view>
#include <string>
#include "LuaBridge/LuaBridge.h"
#include <memory>
#include "fun1.h"
class ABC1{
public:
  ABC1()
  {
    a_=999;
  }


  void set(NotifyMsgType a)
  {
    a_=a;
  }
  void get() const
  {
      std::cout << "a= " << a_ << "!" << std::endl;
  }

private:
  int a_;
};
class ABC2 {
public:
//  ABC2(const std::string name) : m_name(name) {}

  ABC2()
  {
    m_name="22222";
  }
  std::string getName() const {
    return m_name;
  }

  void printName() {
    std::cout << "[C++ CODE] Hello, my name is " << m_name << "!" << std::endl;
  }

  void print(const std::shared_ptr<ABC1>& abc)
  {
    std::cout << "[oid print(std::shared_ptr<ABC1> abc) " << std::endl;
    abc->get();
  }

  void set(const std::string n)
  {
    m_name=n;
  }


private:
  std::string m_name;
};

class Greeter {
public:
  Greeter(const std::string name) : m_name(name) {}

  std::string getName() const {
    return m_name;
  }

  void printName() {
    std::cout << "[C++ CODE] Hello, my name is " << m_name << "!" << std::endl;
  }

  void print(std::shared_ptr<ABC1> abc)
  {
      abc->get();
  }



private:
  std::string m_name;
};


std::shared_ptr<ABC1> foo()
{
  return std::make_shared<ABC1>();
}



void register_xerceslua (lua_State* luaState) {

  // expose the Greeter class to the Lua scripts
  luabridge::getGlobalNamespace(luaState)
      .beginClass<Greeter>("Greeter")
      .addConstructor<void(*) (const std::string&)>()
      .addFunction("getName",&Greeter::getName)
      .addFunction("printName", &Greeter::printName)
      .endClass();

  luabridge::getGlobalNamespace(luaState)
      .beginClass<ABC2>("ABC2")
      .addConstructor<void(*) (void)>()
//      .addConstructor<void(*) (const std::string&)>()
      .addFunction("getName",&ABC2::printName)
      .addFunction("set", &ABC2::set)
      .addFunction("testptr",&ABC2::print)
      .endClass();

  luabridge::getGlobalNamespace(luaState)
      .beginClass<ABC1>("ABC1")
      .addConstructor<void(*) (void)>()
//      .addConstructor<void(*) (const std::string&)>()
      .addFunction("get",&ABC1::get)
      .addFunction("set", &ABC1::set)
      .endClass();

  luabridge::getGlobalNamespace(luaState)
  .beginClass<std::shared_ptr<ABC1>>("ABC1Ptr")
      .endClass().addFunction("getAbcPtr",&foo);
}
